package com.bflow.util;

import com.bflow.api.IBflowDefineActRouteService;
import com.bflow.api.bo.BflowDefineActRouteBo;
import com.bflow.api.bo.BflowRunTaskAuditBo;
import com.bflow.api.bo.callback.BflowCallBackBo;
import com.bflow.api.callback.IBflowCallBackService;

/**
 * @Name 回调工具类
 * @Author haowen
 * @Date 2019-01-09
 */
public class BflowCallBackUtil {


    private static IBflowDefineActRouteService<?, BflowDefineActRouteBo> bflowDefineActRouteService = SpringContextHolder.getBean("bflowDefineActRouteService");

    //进入活动前
    public static void beforeAct(BflowRunTaskAuditBo auditBo){
        BflowDefineActRouteBo routeBo = bflowDefineActRouteService.findById(auditBo.getRouteId());
        String callbackBeforeAct = routeBo.getCallbackBeforeAct();
        call(callbackBeforeAct, routeBo, auditBo);
    }

    //完成活动后
    public static void afterAct(BflowRunTaskAuditBo auditBo){
        BflowDefineActRouteBo routeBo = bflowDefineActRouteService.findById(auditBo.getRouteId());
        String callbackAfterAct = routeBo.getCallbackAfterAct();
        call(callbackAfterAct, routeBo, auditBo);
    }

    private static void call(String callName, BflowDefineActRouteBo routeBo, BflowRunTaskAuditBo auditBo){
        if(HttpUtil.isHttp(callName)){
            callHttp(callName, auditBo);
        }else{
            callService(callName, routeBo, auditBo);
        }
    }

    private static void callService(String serviceName, BflowDefineActRouteBo routeBo, BflowRunTaskAuditBo auditBo){
        BflowCallBackBo callBackBo = new BflowCallBackBo();
        IBflowCallBackService service = SpringContextHolder.getBean(serviceName);
        AssertUtil.isNotNull(service, "回调失败, "+serviceName+" 不存在");

        service.beforeAct(callBackBo);
    }

    private static void callHttp(String url, BflowRunTaskAuditBo auditBo){
//        String resp = HttpUtil.post(url, null, "UTF-8");

    }

}
